package service

import (
	"context"
	config2 "github.com/go-admin-team/go-admin-core/config"
	"github.com/go-admin-team/go-admin-core/sdk/service"
	"github.com/go-pay/gopay"
	"github.com/go-pay/gopay/alipay"
	"net/url"
	"tuowei.com/app/api/service/dto"
	"tuowei.com/pkg/xlog"
)

//const APPID = "2021003126602209"
//const APPID = "2016110702617297" //谭
//公钥
//谭
//MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi5gSDJUx3ggNlh/zqU3CACUaG3LhAVktROEZoilwUe+Bc6J04dqaVqexhtpI7tr/sJGpsQUn64bz6NiJZ/M8oaOHf0c4SrgP2UxaFiyhvcPPHVDbCSkIrhgrSBDMKEP+HiQPUcqe4ArJ0BtuFyA3IbuALX+HKQz6Ipb6avuFvdZRrs2O6g1luIgOOFACmOeCunR269PGvUeUUyKEWwYDo53Nj9dlrhSHDk0OqZUBWzJdhwQpbDJfIlBPV+yFv2V0W9bYGPCnwNoqeZ78L4aBfmOKu6O47IooaOrt9fjUTDdVhuqT0j9HHomIk5DVyeuiwrwQsSh373dW3t7qTauTuwIDAQAB
//应用私钥
//谭
//const privateKey = "MIIEowIBAAKCAQEAi5gSDJUx3ggNlh/zqU3CACUaG3LhAVktROEZoilwUe+Bc6J04dqaVqexhtpI7tr/sJGpsQUn64bz6NiJZ/M8oaOHf0c4SrgP2UxaFiyhvcPPHVDbCSkIrhgrSBDMKEP+HiQPUcqe4ArJ0BtuFyA3IbuALX+HKQz6Ipb6avuFvdZRrs2O6g1luIgOOFACmOeCunR269PGvUeUUyKEWwYDo53Nj9dlrhSHDk0OqZUBWzJdhwQpbDJfIlBPV+yFv2V0W9bYGPCnwNoqeZ78L4aBfmOKu6O47IooaOrt9fjUTDdVhuqT0j9HHomIk5DVyeuiwrwQsSh373dW3t7qTauTuwIDAQABAoIBAGkYs7YPeXzdeUUmhy9XIJE1XcN+qRJ3zlJGWyMwqaS2ejLSA8eq6noFRO3vV+ukq86GzEg/Rn/Xmbkblc/SXDqhw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RxElfqefMfhQdeIbvbJw+TG0aOMMlYomFd7XAo20emtQw6dYZMCgYEAlDKv1zM+k527mnR46rxDKCn20li3pLakSyjst2Y1lhyf8cXjHw2WLrosbNFIX3g4Bc5QHHACtz+Hp8z3MauTq2K9UI4Oc/6Ra4L0rbYMVEzAie1Te6LTR8Jg0BzPJzKIyvtYrYwI3sjdJRMWHsYjENtLqIjv6hHrZx4JQpaEl68CgYAejsZo4dfjaHMqJq3eqztVIXjGVe6rh1LnLovXxyeJ4Bivj0k8AmtNHEv/VI2/Qss1d8fA/XccNX4ft7mGzw4QNhzrppA/3IfJ8QIMtQGykj8U1/bq8zxlcXD9WePtuxvhQHcPzzHMFcgLDHwQxFL3r/0jmcru3peuU9OqK+j7jQKBgB8uiVyiP56MRbA+asKEtqNSIQiI1pqzYKbIs3MpothCZXuIXANf08Mdt3e+NN+qe9Ji3x3Xr7fCArVnm+3/bvWhV+8KLydp6UNXU9FG6BnzAeD0kwsi5aoT6ue5d2v04YTbnn23xn7dmTKzj7/ZZWll1QIh/Whnz4drzgn4yqzV"
//const AliNotifyUrl = "http://test_api.twzxjy.com:8000/api/v1/alipay/callback"
var AliNotifyUrl = "https://api.twzxjy.com/api/v1/alipay/callback"

/*
 拓为教育*/
var APPID = "2021003131655506"
var privateKey = "MIIEpAIBAAKCAQEAycY5oo9M6h+JSyvdkLoYcyGvJ1j25bkjEVE9DLtOSp+jaKaJTteSK0WeAj7FsyT4HI89+QvpDQkS5vzqL2xWbcxT/317/ow+2kIgB3hH5Hc5qwnpi20ATs+7Px9hwYTToN9BrC4Qxo/EfZTD6ZNChAEFYNOn05v3Ct/yhMh+T/tY/ddHjqpjRP0JQ8HDp2c5VCbfifr+FWNmjFTSyTTqJ7tFbLJpWmVITRpoUiF2eL5I4ja4X8SDibHeeucBPPDrBrb/aoupu3uQfznAir5GgJIvn/LroRJcr/NWrlvt38PmyVi1dESZjUTrlA0NaQh0BSSzI7I+ZpB/A9I2GKeX4QIDAQABAoIBAFXP1/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Q2pDml1JMXrd7hLNVbKX4TYKDz6uETIFSzTFewENHpuUMbx2VYDA5gH5jpaCroxLkZG9tFHKO28j+w1Z4IRToUKcNQoiLVdkc/45FhheD++Wi5D1TSm6JA10NQqH7eTxA92MQxiOUhg=="

type AliPay struct {
	service.Service
}

func (e *AliPay) AppPay(ctx context.Context, req *dto.AlipayReq) (string, error) {
	payEnv := config2.Get("PAY_ENV").String("")
	if payEnv == "test" {
		AliNotifyUrl = "http://test_api.twzxjy.com:8000/api/v1/alipay/callback"
		APPID = "2016110702617297"
		privateKey = "MIIEowIBAAKCAQEAi5gSDJUx3ggNlh/zqU3CACUaG3LhAVktROEZoilwUe+Bc6J04dqaVqexhtpI7tr/sJGpsQUn64bz6NiJZ/M8oaOHf0c4SrgP2UxaFiyhvcPPHVDbCSkIrhgrSBDMKEP+HiQPUcqe4ArJ0BtuFyA3IbuALX+HKQz6Ipb6avuFvdZRrs2O6g1luIgOOFACmOeCunR269PGvUeUUyKEWwYDo53Nj9dlrhSHDk0OqZUBWzJdhwQpbDJfIlBPV+yFv2V0W9bYGPCnwNoqeZ78L4aBfmOKu6O47IooaOrt9fjUTDdVhuqT0j9HHomIk5DVyeuiwrwQsSh373dW3t7qTauTuwIDAQABAoIBAGkYs7YPeXzdeUUmhy9XIJE1XcN+qRJ3zlJGWyMwqaS2ejLSA8eq6noFRO3vV+ukq86GzEg/Rn/Xmbkblc/SXDqhwLi9qQ7wJtaF2mlJbvhM/JjosA/keemAvs1CAELsddzBb1s3/odU9/4bOLZmhbt6zBT1J5BrecMjAiQbe+Sg4uqCSrsjxWWp4hSGCybp3OKHLowCX2ClA4Wq5JWbY+tXkNXfI+9pY0lOQqOmFcj33tnsrNej81o+7OjwHQEv/YFAfaO8P9JXpCRvgpMPSIydzeGG54jUkVeCtFIWTk6kZ6c4h5ExglATeUyQvpPMEh2JHppOuJ1zHJuWFDGC6PECgYEAxdkC0Quu2hMqHYK/i7s2k8ameGas8zw5V8xMdZkLWAPScb3ISkqFwjhYQLyFWt9SitY/TWK6WzLB1DVNqHQHjcL7T0bCtSnfN6HyjFCW1gyaSAGDTG+JAY709RLhHkfCnjYRRcQRds6F1bevTRzXBoNQwsaKzM8DncVIOK2la3MCgYEAtJ/HofFIlzVnx124GvGRWPrpk0UjR8/gIFxSBbozsXUTbfjw3Wp+LZHvCQMFEaej3fNKrJ+4Yz7Ow6YWwLB1EDTomBwpRR5JI+Se0peqBIePWxBlH3KHU+AzxEh8ZmBBZsSjFQv/24rYvKYw/kSalyT3sjNUU204hKL34OtZNJkCgYBQVQbxvl6mqTVmehowMDm+0t/w7y+2K3PVFFzPOu8CM7sU98wFbjDVZW9gCavT8+1d9hQJ5dZpBTjyYxo/Fa2jf9HupHqWeIPr4NPM6UP1cXSLXCHlvTvR6CvJ/nptgxGgj69/tlao+L4awtRo6RbdzB/Ulizxb/S18M6cRNVvxQKBgQCq3WtP+pwczlx58GI0usgwUEudEQ4q5K60biD5Ygd8m6/hnFjeW9s46JAjCkRFSzxsvbWqTv1yhPu7gOaNKV/UEPjmy90OkXPHDIJh3S7sUgB+uKewc/sokTLnP1rOzceQm/tZ8Zv75wDYlK5MGkHTMRlP7dabhPquHiw3DOu6IQKBgGEfnvx6ZWDr3FgCEbjaXBAMFDs06PNmE+3uI2TcXsYjVnpM7eY0hSb5R8OoxAfqiUtV5bGpe1fPPVpkid5ef1KiJ0wWH79ohTuSGGa6cXQK3fmAyAbjTXDSlvfO9Pp95HOH99NKuTn+MuFRtY4BTCwHL9S55e5FaVbjE32mvWhM"
	}
	xlog.Infof("AliNotifyUrl bm:%s", AliNotifyUrl)
	// 初始化支付宝客户端
	//    appId：应用ID
	//    privateKey：应用私钥，支持PKCS1和PKCS8
	//    isProd：是否是正式环境
	client, err := alipay.NewClient(APPID, privateKey, true)
	if err != nil {
		return "", err
	}
	// 打开Debug开关，输出日志，默认关闭
	client.DebugSwitch = gopay.DebugOn

	// 设置支付宝请求 公共参数
	//    注意：具体设置哪些参数，根据不同的方法而不同，此处列举出所有设置参数
	client.SetLocation(alipay.LocationShanghai). // 设置时区，不设置或出错均为默认服务器时间
							SetCharset(alipay.UTF8).   // 设置字符编码，不设置默认 utf-8
							SetSignType(alipay.RSA2).  // 设置签名类型，不设置默认 RSA2
							SetNotifyUrl(AliNotifyUrl) // 设置异步通知URL

	// 初始化 BodyMap
	bm := make(gopay.BodyMap)
	bm.Set("subject", req.Subject).
		Set("body", req.Body).
		Set("auth_code", req.ProductCode).
		Set("out_trade_no", req.OrderSn).
		Set("total_amount", req.TotalAmount).
		Set("timeout_express", "2m").
		Set("passback_params", url.QueryEscape(req.OrderType)) //商品类型OnlineOrder，MaterialOrder,LiveOrder
	xlog.Infof("aliapy bm:%+v", bm)
	str, err := client.TradeAppPay(ctx, bm)
	if err != nil {
		xlog.Error(err)
		return "", err
	}
	xlog.Infof("aliapy str:%s", str)
	return str, nil
}
